package com.bookstore.service.impl;

import com.bookstore.dto.BookDTO;
import com.bookstore.entity.Book;
import com.bookstore.entity.Category;
import com.bookstore.repository.BookRepository;
import com.bookstore.repository.CategoryRepository;
import com.bookstore.service.AdminBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AdminBookServiceImpl extends BaseServiceImpl<Book, BookDTO, Long, BookRepository> implements AdminBookService {
    
    @Autowired
    private CategoryRepository categoryRepository;
    
    public AdminBookServiceImpl(BookRepository repository) {
        super(repository);
    }
    
    @Override
    protected BookDTO convertToDTO(Book book) {
        if (book == null) {
            return null;
        }
        
        BookDTO dto = new BookDTO();
        dto.setId(book.getId());
        dto.setTitle(book.getTitle());
        dto.setAuthor(book.getAuthor());
        dto.setPublisher(book.getPublisher());
        dto.setPrice(book.getPrice());
        dto.setOriginalPrice(book.getOriginalPrice());
        dto.setDescription(book.getDescription());
        dto.setImageUrl(book.getImageUrl());
        dto.setStatus(book.getStatus());
        dto.setStock(book.getStock());
        dto.setSalesCount(book.getSalesCount());
        dto.setHotWeight(book.getHotWeight());
        dto.setRecommendWeight(book.getRecommendWeight());
        dto.setFlashSaleWeight(book.getFlashSaleWeight());
        dto.setDegree(book.getDegree());
        
        if (book.getCategory() != null) {
            dto.setCategoryId(book.getCategory().getId());
            dto.setCategoryName(book.getCategory().getCategoryName());
        }
        
        return dto;
    }
    
    @Override
    protected Book convertToEntity(BookDTO dto) {
        Book book = new Book();
        updateEntity(book, dto);
        book.setCreatedAt(LocalDateTime.now());
        return book;
    }
    
    @Override
    protected void updateEntity(Book book, BookDTO dto) {
        book.setTitle(dto.getTitle());
        book.setAuthor(dto.getAuthor());
        book.setPublisher(dto.getPublisher());
        book.setOriginalPrice(dto.getOriginalPrice());
        book.setPrice(dto.getPrice());
        book.setStatus(dto.getStatus());
        book.setStock(dto.getStock());
        book.setDescription(dto.getDescription());
        book.setImageUrl(dto.getImageUrl());
        
        // 更新分类
        if (dto.getCategoryId() != null) {
            Category category = categoryRepository.findById(dto.getCategoryId())
                    .orElseThrow(() -> new RuntimeException("Category not found"));
            book.setCategory(category);
        }
        
        // 更新销量和权重字段
        if (dto.getSalesCount() != null) {
            book.setSalesCount(dto.getSalesCount());
        }
        if (dto.getHotWeight() != null) {
            book.setHotWeight(dto.getHotWeight());
        }
        if (dto.getRecommendWeight() != null) {
            book.setRecommendWeight(dto.getRecommendWeight());
        }
        if (dto.getFlashSaleWeight() != null) {
            book.setFlashSaleWeight(dto.getFlashSaleWeight());
        }
        
        // 更新新旧程度
        if (dto.getDegree() != null) {
            book.setDegree(dto.getDegree());
        }
    }
}